package com.caoji.item.web;

import com.caoji.entity.SpecGroupDTO;
import com.caoji.entity.SpecParamDTO;
import com.caoji.item.service.SpecGroupService;
import com.caoji.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecGroupService groupService;
    @Autowired
    private SpecParamService paramService;

    //根据商品分类id，查询规格组的集合
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querybycateoryid(@RequestParam("id")Long id){
        return ResponseEntity.ok(groupService.querybycateoryid(id));
    }

    //根据商品分类id，查询规格组的集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querybybrandid(
            @RequestParam(value = "groupId",required = false)Long gid,
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching){
        return ResponseEntity.ok(paramService.querybybrandid(gid,cid,searching));
    }

    //根据条件查询规格参数集合
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querybycid(
            @RequestParam("id")Long cid){
        return ResponseEntity.ok(groupService.querybycid(cid));
    }

    //新增规格租
    @PostMapping("/group")
    public ResponseEntity<Void> addgroup(@RequestBody SpecGroupDTO groupDTO){
        groupService.addgroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //新增规格租
    @PutMapping("/group")
    public ResponseEntity<Void> updategroup(@RequestBody SpecGroupDTO groupDTO){
        groupService.updategroup(groupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<Void> addparam(@RequestBody SpecParamDTO paramDTO){
        paramService.addparam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //新增规格参数
    @PutMapping("/param")
    public ResponseEntity<Void> updateparam(@RequestBody SpecParamDTO paramDTO){
        paramService.updateparam(paramDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}